﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Irvin_Engine.Camera;

namespace Irvin_Engine.Sound
{
    public class SoundManager
    {
        Dictionary<string, Sound> sounds = new Dictionary<string, Sound>();
        Dictionary<string, Music> musics = new Dictionary<string, Music>();

        private static SoundManager m_oInstance;
        float distMaxPan = 1024.0f;

        Music currentMusic = null;

        private SoundManager()
        {
        }

        private void ComputePan(Vector2 _posEntity)
        {
            float distCamEntity = (_posEntity - CameraManager.GetInstance().CameraCurrent.Position).Length() / distMaxPan;

            if (distMaxPan > 1.0f)
            {
                distMaxPan = 1.0f;
            }
            else if (distMaxPan < -1.0f)
            {
                distMaxPan = -1.0f;
            }
        }

        public static SoundManager GetInstance()
        {
            if (m_oInstance == null)
            {
                m_oInstance = new SoundManager();
            }
            return m_oInstance;
        }

        public void RegisterSounds(string[] _names)
        {
            for (int i = 0; i < _names.Length; i++)
            {
                sounds.Add(_names[i], new Sound()); //TODO: à mettre les paramètres dans le constructeur de sons et de musiques
            }
        }

        public void RegisterOneSound(string _name, float _volume)
        {
            sounds.Add(_name, new Sound(_name, _volume, 0.0f, 0.0f));
        }

        public void RegisterOneMusic(string _name, float _volume)
        {
            musics.Add(_name, new Music(_name, _volume));
        }

        public void RegisterMusics(string[] _names, float _volume)
        {
            for (int i = 0; i < _names.Length; i++)
            {
                musics.Add(_names[i], new Music(_names[i], _volume));
            }
        }

        public void PlaySound(string _nameSound, bool _withPan)
        {
            for(int i = 0; i < sounds[_nameSound].SoundInstances.Count; i++)
                sounds[_nameSound].SoundInstances[i].Play();
        }

        public void PlayAMusic(string _nameMusic)
        {
            if (currentMusic != null)
            {
                currentMusic.Stop();
            }
            currentMusic = musics[_nameMusic];
            currentMusic.Play();
        }
    }
}
